require 'View/UIBase/UIBase'

UILoginAward = UIBase.New(PanelResNames.UILoginAward)

function UILoginAward.Init(func, id)
    UILoginAward.activeId = id;
    UIBase.Init(UILoginAward,func);
end

-- 创建回调
function UILoginAward:OnCreate()
    self.countDownNum = 0;

    self.bg = self:GetChild("bg");
    self.bgText = self:GetChild("bgText");
    self.bgTitle = self:GetChild("Tips100");
    self.bgSp = self:GetChild("bgsp");
    self.titleTop = self:GetChild("titleTop");
    self.bgTop = self:GetChild("bgTop");
    self.activityTitleIcon = self:GetChild("activityTitleIcon");
    self.heroIcon = self:GetChild("HeroIcon");
    self.titleText = self:GetChild("titleText");
    self.getBtn = self:GetChild("get");
    self.getBtn.title = getLanguage("button_01");
    self.tipTime = self:GetChild("tipTime");
    self.tipDecs2 = self:GetChild("tipDecs2");
    self.title = self:GetChild("title");
    self.singleAwardList = self:GetChild("singleAwardList");
    self.typeController = self:GetController("type");
    self.overController = self:GetController("IsOver");
    self.grayController = self:GetController("gray");
    self.tips = self:GetChild("tips");
    self.spine_effect = self:GetChild("effect_taohua_dtyf");
    self.awardList = self:SetDynamicList(self:GetChild("AwardList"),self.updateItem);
    --self.specialAwardList = self:SetDynamicList(self:GetChild("AwardList100"),self.updateItem);
    self.specialAwardList = self:GetChild("AwardList100");
    self.specialAwardList.itemRenderer = self.updateSpecialItem;
    self.activityList = {};
    for i = 1, 7 do
        table.insert(self.activityList, self:GetChild("ActivityList"):GetChild("Day"..i));
    end

    self:addEvent();
end

-- 显示
function UILoginAward:OnEnable()
    if self.activeId == nil then
        self:OnBack();
    else
        logicMgr.HeroManager.setGetNewHeroShow(true);
        self:Refresh();
    end
end

function UILoginAward:addEvent()
    self:RegisterOnClick(self:GetChild("bgClose"),self.OnClose);
    self:RegisterOnClick(self:GetChild("get"),self.OnGet);
    self:RegisterEvent(Config.EventType.Fresh_Active,self.Refresh);
end

function UILoginAward:removeEvent()
end

function UILoginAward:Refresh()
    self:UpdateData();
end

function UILoginAward:UpdateData()
    self.tableData, self.serverData = logicMgr.ActiveManager.getActiveData(self.activeId);
    if self.tableData == nil or self.serverData == nil or not self.serverData.isShow then
        self:OnBack();
        return ;
    end
    self.day = logicMgr.ActiveManager.getActiveBeginDay(self.serverData.beginTime);
    if self.tableData.f_BgPic ~= nil and self.tableData.f_BgPic ~= "" then
        self.bg.url = tostring(self.tableData.f_BgPic);
    end
    self.bgText.url = tostring(self.tableData.f_HeadPic);
    if self.tableData.f_TitlePic ~= nil and self.tableData.f_TitlePic ~= "" then
        self.bgTitle.url = tostring(self.tableData.f_TitlePic);
    end
    if self.tableData.f_BgPicSp ~= nil and self.tableData.f_BgPicSp ~= "" then
        self.bgSp.url = tostring(self.tableData.f_BgPicSp);
    end
    if self.tableData.f_BgPicTop ~= nil and self.tableData.f_BgPicTop ~= "" then
        self.bgTop.url = tostring(self.tableData.f_BgPicTop);
    end
    if self.tableData.f_HeroIcon ~= nil and self.tableData.f_HeroIcon ~= "" then
        self.heroIcon.url = tostring(self.tableData.f_HeroIcon);
    end
    if self.tableData.f_activityTitleIcon ~= nil and self.tableData.f_activityTitleIcon ~= "" then
        self.activityTitleIcon.url = tostring(self.tableData.f_activityTitleIcon);
    end

    self.titleTop.text = getLanguage(self.tableData.f_Title);
    self.titleText.text = getLanguage(self.tableData.f_TitleDesc);
    local activeType = tonumber(self.tableData.f_Type);
    if activeType == 0 then
        self.typeController.selectedIndex = 0;
        if self.serverData.receive[1] == -1 then
            self.grayController.selectedIndex = 1;
        else
            self.grayController.selectedIndex = 0;
        end
    else
        self.typeController.selectedIndex = activeType;
    end
    self:loadEffect();
    self.countDownNum = self.serverData.endTime - HelperFunc.GetServerTime();
    self:RemoveTimeCall(self.OnCountDown);
    if self.countDownNum <= 0 then
        self.overController.selectedIndex = 0;
        self.tipDecs2.text = getLanguage("Sys_ActivityEnd");
    else
        self.overController.selectedIndex = 1;
        self:TimeCall(self.OnCountDown, 1000, -1);
        self.tipTime.text = string.format(getLanguage("Sys_ActivityTimeLeft"), HelperFunc.GetSurpTimeStr(self.countDownNum));
    end
    if activeType == 1 then
        self.awardList.numItems = self.tableData.f_DayTime;
    elseif activeType == 2 then
        self.specialAwardList.numItems = self.tableData.f_DayTime;
    elseif activeType == 3 then
        self.tips.text = getLanguage("ActivityListTaohuaTips6");
        for i = 1, #self.activityList do
            local activityListIndex = i;
            self:updateItem(activityListIndex, self.activityList[activityListIndex]);
        end
    else
        self.title.text = getLanguage(self.tableData.f_TitleAward[1]);
        logicMgr.CostManager.RefreshItemList(self.singleAwardList, self.tableData.f_Rewards[1]);
    end
end

function UILoginAward:loadEffect()
    if self.tableData.f_spine ~= nil and self.tableData.f_spine ~= "" then
        local list = string_split(self.tableData.f_spine,"/")
        AddUIPackageDepend(PanelResNames.UILoginAward,"FUI/"..list[1]);
        self.spine_effect.url = "ui://"..self.tableData.f_spine;
        --resMgr:LoadModelPrefab(self.tableData.f_spine, function(go)
        --    if self.shuiPaoDrawWrapper == nil then
        --        self.shuiPaoDrawWrapper = GoWrapper.New();
        --    end
        --    if (self.shuiPaoObj~= nil) then
        --        destroyImmediate(self.shuiPaoObj);
        --    end
        --    self.shuiPaoObj = newObject(go);
        --    self.shuiPaoObj:AddComponent(TCanvas);
        --    self.shuiPaoObj:AddComponent(TChangeSortingOrder)
        --    self.shuiPaoDrawWrapper:SetWrapTarget(self.shuiPaoObj, false)
        --    Util.Lua_SetLocalPositionXYZ(self.shuiPaoObj.transform, 0, 0, 0)
        --    self.spine_effect:SetNativeObject(self.shuiPaoDrawWrapper);
        --end)
    end
end

function UILoginAward.updateSpecialItem(index, itemObj)
    UILoginAward:updateItem(index + 1, itemObj);
end

function UILoginAward:updateItem(index, itemObj)
    local serverData = self.serverData.receive[index];
    local rewards = self.tableData.f_Rewards[index];
    local title = itemObj:GetChild("title");
    local list = itemObj:GetChild("list");
    local typeController = itemObj:GetController("IsGet");
    local btnGet = itemObj:GetChild("get");

    local activeType = tonumber(self.tableData.f_Type);
    if activeType == 1 or activeType == 3 then
        local btnBg = itemObj:GetChild("bg");
        local mask = itemObj:GetChild("mask");
        if self.tableData.f_BtnBg ~= nil and self.tableData.f_BtnBg ~= "" then
            btnBg.url = self.tableData.f_BtnBg;
            mask.url = self.tableData.f_BtnBg;
        end
        local btnBgGet = itemObj:GetChild("bgget");
        if self.tableData.f_BtnBgGet ~= nil and self.tableData.f_BtnBgGet ~= "" then
            btnBgGet.url = self.tableData.f_BtnBgGet;
        end
        local textTitle = itemObj:GetChild("title1");
        textTitle.text = getLanguage("ActivityListTaohuaTips7");
        textTitle = itemObj:GetChild("title3");
        textTitle.text = getLanguage("ActivityListTaohuaTips8");
    end

    if self.tableData.f_TitleAward ~= nil and self.tableData.f_TitleAward ~= "" then
        title.text = getLanguage(self.tableData.f_TitleAward[index], nil, "");
    else
        title.text = tostring(index);
    end

    btnGet.onClick:Clear();
    itemObj.onClick:Clear();
    if serverData == -1 then
        typeController.selectedIndex = 2;
    elseif serverData == 0 then
        typeController.selectedIndex = 0;
    elseif serverData == 1 then
        typeController.selectedIndex = 1;
        if activeType == 1 or activeType == 3 then
            itemObj.onClick:Add(function()
                self:OnGet(nil, index);
            end, self);
        elseif activeType == 2 then
            btnGet.onClick:Add(function()
                self:OnGet(nil, index);
            end, self);
        end
    else
        typeController.selectedIndex = 1;
    end
    list:RemoveChildrenToPool();
    
    local num = table.getn(rewards);
    --[[
    if self.tableData.f_DayTime > 1 then
        num = 1;
    end
    --]]
    for i = 1, num do
        local item = list:AddItemFromPool();
        local itemList = item:GetChild("itemList");
        itemList:RemoveChildrenToPool();
        local rewardItem = itemList:AddItemFromPool();
        logicMgr.CostManager.RefreshItem(rewardItem, rewards[i]);
        local itemListController = item:GetController("IsGet");
        itemListController.selectedIndex = 1;
        if serverData == -1 then
            itemListController.selectedIndex = 2;
        end
    end
    --itemList.width = (itemList.columnGap + itemList:GetChildAt(0).width) * num - itemList.columnGap;
end

--- 倒计时
function UILoginAward:OnCountDown()
    self.countDownNum = self.countDownNum - 1;
    local tip_time_contetnt = string.format(getLanguage("Sys_ActivityTimeLeft"), HelperFunc.GetSurpTimeStr(self.countDownNum))
    if self.cur_tip_time_content == nil or self.cur_tip_time_content ~= tip_time_contetnt then
        self.tipTime.text = tip_time_contetnt
        self.cur_tip_time_content = tip_time_contetnt
    end
    if self.countDownNum <= 0 then
        self:RemoveTimeCall(self.OnCountDown);
        self.overController.selectedIndex = 0;
        self.tipDecs2.text = getLanguage("Sys_ActivityEnd");
    end
end

--- 领取
function UILoginAward:OnGet(fgui, index)
    if index == nil then
        index = 1;
    end
    ActivityHandler.sendReqActivityDrawRewardMessage({self.activeId, index});
end

-- 关闭
function UILoginAward:OnClose()
    self:OnBack();
    self:RemoveAllTimeCall();
    logicMgr.ActiveManager.showPopUpTips();
end

-- 隐藏
function UILoginAward:OnDisable()
    logicMgr.HeroManager.setGetNewHeroShow();
end

-- 卸载
function UILoginAward:OnDestroy()
    self:removeEvent();
    --if self.shuiPaoObj ~= nil then
    --    destroy(self.shuiPaoObj)
    --    self.shuiPaoObj = nil
    --end
    --if self.shuiPaoDrawWrapper ~= nil then
    --    self.shuiPaoDrawWrapper:Dispose();
    --    self.shuiPaoDrawWrapper = nil;
    --end
end

return UILoginAward